﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using NLBS.Web;
using NLBS.Entity;
using System.Collections.Generic;
using System.Reflection;
using NLBS.Data.Business;
using NLBS.Framework;

namespace NLBS.Control
{
    public partial class PanelList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            XmlDocument doc = SiteConfig.GetWidgetXml();
            XmlNodeList nodelist = doc.SelectSingleNode("WidgetList").ChildNodes;

            List<Widget> list = WidgetData.GetWidget().FindAll(delegate(Widget w)
                {
                    return w.Hidden == false;
                });
            foreach (Widget item in list)
            {
                string content = (string)item.Content.Clone();
                foreach (XmlNode node in nodelist)
                {
                    string nameValue = node.Attributes["Name"].Value;

                    if (content.Contains(nameValue))
                    {
                        string className = node.Attributes["ClassName"].Value;
                        string b = string.Empty;
                        if (className.EndsWith("ascx"))
                        {

                            b = ViewManager.RenderView(LoadControl(RequestWrapper.ApplicationPath + "//" + className));
                        }
                        else
                        {
                            Assembly a = Assembly.Load(node.Attributes["Assembly"].Value);
                            b = ViewManager.RenderView((System.Web.UI.Control)a.CreateInstance(className));
                        }
                        content = item.Content.Replace(nameValue, b);
                    }
                }
                this.Controls.Add(new LiteralControl(string.Format("<div id=\"{0}\" class=\"panel\"><h5>{1}</h5>{2}</div>", item.Name, item.Title, content)));
            }
        }
    }
}